﻿
#region NameSpaces
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
#endregion

public partial class Admin_ProxAnalysis : System.Web.UI.Page
{
    Service objService = new Service();

    #region UI events

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillProximateAnalysisDetails(Convert.ToInt32(id));
                txtProID.Text = string.Empty;
                trDgProximateAnalysis.Visible = false;
                trProxAnal.Visible = true;
            }

            fillDataGrid();
        }
    }
    private void ClearTextboxes()
    {
        txtFixedCarbon.Text = string.Empty;
        txtMoisture.Text = string.Empty;
        txtProID.Text = string.Empty;
        txtVolatiles.Text = string.Empty;
        txtAsh.Text = string.Empty;

    }

    protected void dgProxanal_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgProxanal.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillProximateAnalysisDetails(id);
        trProxAnal.Visible = true ;
        trDgProximateAnalysis.Visible = false ;
    }

    private void fillProximateAnalysisDetails(Int32 Id)
    {

        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("proxanal", Id);

        txtAsh.Text = ds.Tables[0].Rows[0]["ash"].ToString();
        txtFixedCarbon.Text = ds.Tables[0].Rows[0]["fixedCarbon"].ToString();
        txtProID.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtVolatiles.Text = ds.Tables[0].Rows[0]["volatiles"].ToString();
        txtMoisture.Text = ds.Tables[0].Rows[0]["moisture"].ToString();
        ddlMoistureStatus.SelectedValue = ds.Tables[0].Rows[0]["moistureStatus"].ToString();
    }

    protected void dgProxanal_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgProxanal.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }

    #endregion

    #region Private Methods

    private void fillDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId == null)
        {
            lblMessage.Text = "You haven't entered any record for the Proximate analysis to this experiment";
            trProxAnal.Visible = true;
            trDgProximateAnalysis.Visible = false;
        }
        else
        {
            Int32 ProxAnalId = objService.GetExperimentFKValues("proxanal_fk", ExpId);
            if (ProxAnalId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any record for the Proximate analysis to this experiment";
                trProxAnal.Visible = true;
                trDgProximateAnalysis.Visible = false;
            }
            else
            {
                if (objService.GetProximateAnalysis(ProxAnalId.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgProxanal.DataSource = objService.GetProximateAnalysis(ProxAnalId.ToString());
                    dgProxanal.DataBind();
                    trProxAnal.Visible = false;
                    trDgProximateAnalysis.Visible = true;
                }
                else
                {
                    trProxAnal.Visible = true;
                    trDgProximateAnalysis.Visible = false;
                }
            }
        }
    }

  

    #endregion



    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
           string ReferenceID = (string)Session["ReferenceId"];

            if (txtProID.Text != string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("proxanal", "moistureStatus", ddlMoistureStatus.SelectedValue.ToString(), txtProID.Text);
                if (txtMoisture.Text == string.Empty)
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "moisture", "null", txtProID.Text);
                }
                else
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "moisture", txtMoisture.Text, txtProID.Text);
                }

                if (txtAsh.Text == string.Empty)
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "ash", "null", txtProID.Text);
                }
                else
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "ash", txtAsh.Text, txtProID.Text);
                }

                if (txtVolatiles.Text == string.Empty)
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "volatiles", "null", txtProID.Text);
                }
                else
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "volatiles", txtVolatiles.Text, txtProID.Text);
                }

                if (txtFixedCarbon.Text == string.Empty)
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "fixedCarbon", "null", txtProID.Text);
                }
                else
                {
                    lblMessage.Text = objService.UpdateTable("proxanal", "fixedCarbon", txtFixedCarbon.Text, txtProID.Text);
                }

                fillDataGrid();

                ClearTextboxes();

            }
            else
            {
                lblMessage.Text = objService.SaveProximityAnalysis(ddlMoistureStatus.SelectedValue.ToString(), txtMoisture.Text, txtAsh.Text, txtVolatiles.Text, txtFixedCarbon.Text);

                string ProximateAnalysisId = objService.GetLastInsertedId("ref_fk", ReferenceID, "proxanal");

                objService.UpdateTable("proxanal", "ref_fk", ReferenceID, ProximateAnalysisId);

                ClearTextboxes();

                if ((string)Session["ExperimentId"] != null)
                {
                    string ExpId = (string)Session["ExperimentId"];
                    if (objService.UpdateExperiment("proxanal_fk", ProximateAnalysisId, ExpId) != "Experiment Successfully Updated")
                    {
                        objService.DeleteRecord("proxanal", Convert.ToInt32(ProximateAnalysisId));

                        lblMessage.Text = "Admin - You can't enter the multiple Proximity analysis records for one experiment";

                        fillDataGrid();
                    }
                    else
                    {
                        lblMessage.Text = "Proximate Analysis Saved and " + objService.UpdateExperiment("proxanal_fk", ProximateAnalysisId, ExpId);

                        fillDataGrid();
                    }

                    lblExpdetails.Text = "Your Experiment Id is - " + ExpId;
                }
                else
                {

                    lblMessage.Text = "Proximate Analysis Saved and " + objService.SaveExperiment(null, null, null, null, null, null, null, ProximateAnalysisId, null, null, null, ReferenceID, null);
                    string ExperimentId = objService.GetExperimentId("proxanal_fk", ProximateAnalysisId);
                    Session["ExperimentId"] = ExperimentId;

                    lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;

                    fillDataGrid();
                }

            }
        }
    
    protected void btnPreviousRecord_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreProximateAnalysis.aspx");
    }
}